package com.it135.citytraffic.task;

import com.it135.citytraffic.pojo.Ming;
import com.it135.citytraffic.pojo.Xing;
import com.it135.citytraffic.pojo.entity.Survey;
import com.it135.citytraffic.service.ISurveyService;
import lombok.RequiredArgsConstructor;
import org.springframework.ai.chat.model.ChatResponse;
import org.springframework.ai.chat.prompt.Prompt;
import org.springframework.ai.openai.OpenAiChatModel;
import org.springframework.ai.openai.OpenAiChatOptions;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;
import java.util.*;

@Component
@RequiredArgsConstructor
public class AddTask {

    private final ISurveyService surveyService;
    private final OpenAiChatModel openAiChatModel;

    @Scheduled(cron = "${task.addScheduled.cron}")
    public void add() {
        List<String> list = List.of("1.您的年龄范围是？", "3.您所在的地区", "4.您平时的主要出行方式是？", "1.您认为成都交通拥堵的主要原因是什么？",
                "2.您认为这些问题严重程度如何？", "3.您通常在哪个时间段遇到交通拥堵的情况最频繁", "4.您认为成都哪些区域交通拥堵问题最为严重?",
                "1.您是否经常使用公共交通工具？", "2.如果否，阻碍您使用公共交通工具的主要原因是什么？", "您认为城市在缓解交通拥堵问题上应该优先考虑的方面是？");

        HashMap<String, List<String>> hm = new HashMap<>();
        hm.put(list.get(0), List.of("A. 18-25岁","B. 26-35岁","C. 36-45岁","D. 46岁及以上"));
        hm.put(list.get(1), List.of("A. 锦江区","B. 青羊区","C. 金牛区","D. 武侯区","E. 成华区",
                "F. 龙泉驿区","G. 青白江区","H. 新都区","I. 温江区","J. 双流区","K. 郫都区","L. 新津区"));
        // 多选
        hm.put(list.get(2), List.of("A. 私家车","B. 公交车","C. 地铁","D. 出租车/网约车","E. 自行车/电动车",
                "F. 步行"));
        // 多选
        hm.put(list.get(3), List.of("A. 红绿灯设置不合理","B. 拥堵路段管理不当","C. 路段导流不畅","D. 停车不便",
                "E. 道路施工","F. 交通信号灯故障","G. 天气等不可抗力因素","H. 车辆数量过多",
                "I. 公共交通设施不足（或公共交通站点设置不合理）"));
        hm.put(list.get(4), List.of("A. 非常严重","B. 比较严重","C. 一般","D. 不太严重","E. 不严重"));
        // 多选
        hm.put(list.get(5), List.of("A. 早上上班高峰（7:00-9:00）","B. 中午时段（11:00-14:00）",
                "C. 下午下班高峰（17:00-19:00）","D. 晚上休闲娱乐时段","E. 周末或节假日"));
        // 多选
        hm.put(list.get(6), List.of("A. 商业中心","B. 学校周边","C. 医院附近","D. 居民区周边",
                "E. 交通枢纽（火车站、汽车站）"));
        hm.put(list.get(7), List.of("A. 是","B. 否"));
        // 多选
        hm.put(list.get(8), List.of("A. 公共交通站点距离设置不当","B. 公共交通线路设置不当",
                "C. 公共交通工具的发车频率低，等待时间长"));
        hm.put(list.get(9), List.of("A. 建设更多交通道路","B. 发展公共交通","C. 鼓励非机动车出行",
                "D. 限制机动车数量"));

        String[] str = {"您对城市交通管理还有哪些建议？","您认为如何才能更好地缓解交通拥堵问题？","您希望未来城市交通发展成什么样子？"};
        Random random = new Random();
        HashSet<String> hs = new HashSet<>();


        List<Survey> surveyList = new ArrayList<>();
        for (int f = 0; f < random.nextInt(80,130); f++) {
            String name = Xing.getXing() + Ming.getMing();
            LocalDateTime time = LocalDateTime.now();

            for (int i = 0; i < list.size(); i++) {
                if (i == 2 || i == 3 || i == 5 || i == 6 || i == 8) {
                    StringBuilder sb = new StringBuilder();
                    int j = random.nextInt(1, hm.get(list.get(i)).size());
                    for (int k = 0; k < j; k++) {
                        hs.add(hm.get(list.get(i)).get(random.nextInt(hm.get(list.get(i)).size())));
                    }
                    List<String> answerList = hs.stream().toList();
                    for (int k = 0; k < answerList.size(); k++) {
                        if (k == answerList.size() - 1) {
                            sb.append(answerList.get(k));
                        } else
                            sb.append(answerList.get(k)).append(";");
                    }
                    hs.clear();
                    String answer = sb.toString();

                    Survey survey = new Survey();
                    survey.setSurveyId(1);
                    survey.setName(name);
                    survey.setCreateTime(time);
                    survey.setQuestion(list.get(i));
                    survey.setAnswer(answer);
                    surveyList.add(survey);
                } else {
                    String question = list.get(i);
                    List<String> answerList = hm.get(question);
                    String answer = answerList.get(random.nextInt(hm.get(question).size()));

                    Survey survey = new Survey();
                    survey.setSurveyId(1);
                    survey.setName(name);
                    survey.setCreateTime(time);
                    survey.setQuestion(question);
                    survey.setAnswer(answer);
                    surveyList.add(survey);
                }
            }

            for (String question : str) {
                String answer = call(question + "回答这个问题，字数控制在50字以内");
                Survey survey = new Survey();
                survey.setSurveyId(1);
                survey.setName(name);
                survey.setCreateTime(time);
                survey.setQuestion(question);
                survey.setAnswer(answer);
                surveyList.add(survey);
            }

        }
        surveyService.saveBatch(surveyList);
    }

    private String call(String question) {
        OpenAiChatOptions openAiChatOptions = OpenAiChatOptions.builder()
                .withTemperature(0.8F)
                .build();
        Prompt prompt = new Prompt(question, openAiChatOptions);
        ChatResponse answer = openAiChatModel.call(prompt);
        return answer.getResult().getOutput().getContent();
    }
}
